﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using static TMPro.SpriteAssetUtilities.TexturePacker_JsonArray;


public static class MediaSystemBind
{
    static AndroidJavaObject g_context = null;
    public static AndroidJavaObject Context
    {
        get
        {
            if (g_context == null)
            {
                using (AndroidJavaClass root = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                {
                    g_context = root.GetStatic<AndroidJavaObject>("currentActivity");
                    bool b = g_context == null;
                    Debug.LogWarning("====b=" + b);
                }
            }
            return g_context;
        }
    }
    static AndroidJavaClass _g_classobj=null;
    static AndroidJavaClass ClassObj
    {
        get
        {
            if (_g_classobj == null)
            {
                _g_classobj = new AndroidJavaClass("ml.exp.MediaSystem");
            }
            return _g_classobj;

        }
    }

    public static bool Support
    {
        get
        {
            if (Application.isEditor)
                return false;
            if (Application.platform != RuntimePlatform.Android)
                return false;

            return true;

        }
    }
    public static void System_Start()
    {

        ClassObj.CallStatic("System_Start", Context);
    }
    public static bool System_IsOpen()
    {
        return ClassObj.CallStatic<bool>("System_IsOpen");
    }
    public static void System_Stop()
    {
        ClassObj.CallStatic("System_Stop");
    }
    public static void System_Pause()
    {
        ClassObj.CallStatic("System_Pause");

    }
    public static void System_Resume()
    {
        ClassObj.CallStatic("System_Resume");

    }
    public static bool System_IsPause()
    {
        return ClassObj.CallStatic<bool>("System_IsPause");

    }
    public enum MediaMode
    {
        StramingAssets = 1,
        Url = 2,
    }

    public static int Media_Open(MediaMode mode, string name, bool autoplay)
    {
        return ClassObj.CallStatic<int>("Media_Open", (int)mode, name, autoplay);
    }
    public static void CloseMediaAndView(int mediaid)
    {
        ClassObj.CallStatic("CloseMediaAndView", mediaid);
    }

    public static void Media_SetVolume(int mediaid, float vol)
    {
        ClassObj.CallStatic("Media_SetVolume", mediaid, vol);
    }

    public static void Media_SetLoop(int mediaid, bool v)
    {
        ClassObj.CallStatic("Media_SetLoop", mediaid, v);
    }

    public static void Media_OpenView(int mediaid, int w = 0, int h = 0)
    {
        ClassObj.CallStatic("Media_OpenView", mediaid, w, h);
    }
    public static bool Media_HasView(int mediaid)
    {
        return ClassObj.CallStatic<bool>("Media_HasView", mediaid);
    }

    [DllImport("javaaddr")]
    //[DllImport("__Internal")]
    private static extern IntPtr GetJniEnv();

    //[DllImport("__Internal")]
    [DllImport("javaaddr")]
    private static extern IntPtr GetAddr(IntPtr javaobj);

    public static IntPtr Media_GetViewBuffer(int mediaid)
    {
        var obj = ClassObj.CallStatic<AndroidJavaObject>("Media_GetViewBuffer", mediaid);
        return GetAddr(obj.GetRawObject());
    }
    public static int Media_GetViewWidth(int mediaid)
    {
        return ClassObj.CallStatic<int>("Media_GetViewWidth", mediaid);
    }
    public static int Media_GetViewHeight(int mediaid)
    {
        return ClassObj.CallStatic<int>("Media_GetViewHeight", mediaid);
    }
}

